package api

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxsop-web/user-web/proto/proto_go"
)

func GetUserList(ctx *gin.Context) {
	ip := "127.0.0.1"
	port := 5051
	//拨号连接用户grpc服务
	conn, err := grpc.NewClient(fmt.Sprintf("%s:%d", ip, port), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Error("[GetUserList]连接【用户服务失败】",
			"msg", err.Error())
	}
	//生成grpc的client
	client := proto_go.NewUserClient(conn)
	pageInfo := new(proto_go.PageInfo)
	pageInfo.Pn = 1
	pageInfo.PSize = 10
	rsp, err2 := client.GetUserList(context.Background(), pageInfo)
	if err2 != nil {
		zap.S().Error("[GetUserList]请求【用户服务-用户列表失败】",
			"msg", err.Error())
	}
	zap.L().Debug("获取用户列表页")

	ctx.JSON(200, gin.H{
		"msg":  "请求成功",
		"data": rsp,
	})
}
